{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 基本操作"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数组创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "code_folding": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5]\n"
     ]
    }
   ],
   "source": [
    "#列表转换成数组\n",
    "l = [i+1 for i in range(5)]\n",
    "nd1 = np.array(l)\n",
    "print(nd1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0, 0],\n",
       "       [0, 0, 0, 0],\n",
       "       [0, 0, 0, 0]], dtype=int16)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd2 = np.zeros(shape = (3,4),dtype = np.int16)\n",
    "nd2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1., 1.]], dtype=float32)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd3 = np.ones(shape = (3,5),dtype = np.float32)\n",
    "nd3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],\n",
       "        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],\n",
       "        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],\n",
       "        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926]],\n",
       "\n",
       "       [[3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],\n",
       "        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],\n",
       "        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],\n",
       "        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926]],\n",
       "\n",
       "       [[3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],\n",
       "        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],\n",
       "        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],\n",
       "        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926]]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd4 = np.full(shape=(3,4,5),fill_value = 3.1415926)\n",
    "nd4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([96, 23,  4, 82, 15, 75, 49, 76, 54, 32, 81, 50, 81, 33, 12, 15, 23,\n",
       "       11, 35, 42])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd5 = np.random.randint(0,100,size = 20)\n",
    "nd5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.54223389, 0.87362588, 0.18420378, 0.54290381, 0.78828669],\n",
       "       [0.31721619, 0.74219741, 0.3847374 , 0.19977502, 0.0013446 ],\n",
       "       [0.42816899, 0.03497441, 0.61690552, 0.75069059, 0.9818437 ]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd6 = np.random.rand(3,5)#生成0-1之间的随机数\n",
    "nd6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.26333864,  0.49445851, -1.07444574,  0.01135711, -0.6580713 ],\n",
       "       [-2.07456006, -1.30674579, -0.77036343, -0.49100304,  0.38002957],\n",
       "       [-0.47045137,  0.11309118, -0.48871501,  0.08110288,  1.3493228 ]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n7 = np.random.randn(3,5)#正态分布，0-1\n",
    "n7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[191.55309359, 177.83122477, 182.06583467, 176.44870981,\n",
       "        177.84302064],\n",
       "       [172.66394492, 178.61506315, 181.17570592, 183.14285148,\n",
       "        175.41464775],\n",
       "       [185.13376346, 175.18438725, 181.76224173, 181.40279872,\n",
       "        181.60272937]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n8 = np.random.normal(loc = 175,scale = 10,size =(3,5)) #平均值175，标准差10\n",
    "n8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1, 11, 21, 31, 41, 51, 61, 71, 81, 91])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n9 = np.arange(1,100,step = 10)#等差数列,左闭右开\n",
    "n9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10., 11., 12.,\n",
       "       13., 14., 15., 16., 17., 18., 19., 20., 21., 22., 23., 24., 25.,\n",
       "       26., 27., 28., 29., 30., 31., 32., 33., 34., 35., 36., 37., 38.,\n",
       "       39., 40., 41., 42., 43., 44., 45., 46., 47., 48., 49., 50., 51.,\n",
       "       52., 53., 54., 55., 56., 57., 58., 59., 60., 61., 62., 63., 64.,\n",
       "       65., 66., 67., 68., 69., 70., 71., 72., 73., 74., 75., 76., 77.,\n",
       "       78., 79., 80., 81., 82., 83., 84., 85., 86., 87., 88., 89., 90.,\n",
       "       91., 92., 93., 94., 95., 96., 97., 98., 99.])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd10 = np.linspace(0,99,100)#等差数列，左闭右闭\n",
    "nd10"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###安装相关插件-可视化大纲等"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "pip install jupyter_contrib_nbextensions -i https://pypi.tuna.tsinghua.edu.cn/simple\n",
    "pip install jupyter_nbextensions_configurator -i https://pypi.tuna.tsinghua.edu.cn/simple\n",
    "jupyter contrib nbextension install --user\n",
    "jupyter nbextensions_configurator enable --user\n",
    "#配置后，选中table of contents,codefolding,hinterland"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 查看数组属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.37963397,  1.76665447,  0.14457821],\n",
       "       [ 0.69027629, -0.25568341,  0.61767037],\n",
       "       [ 0.898221  ,  0.93898819,  0.71648477],\n",
       "       [ 0.29763321,  0.7648314 , -0.1655371 ],\n",
       "       [ 0.85532309,  0.58158296,  0.92790593]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "nd = np.random.randn(5,3)\n",
    "nd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5, 3)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#查看数组形状\n",
    "nd.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('float64')"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd.dtype  #数组的数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "15"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd.size #数组可以是多维的，里面共有多少数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd.ndim #数组维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd.itemsize #条目尺寸长度（8个字节）\n",
    "#float64位，一个字节是8位"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 文件读写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "nd1 = np.random.randint(0,100,size = (3,5))\n",
    "nd2 = np.random.randn(3,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.save('./data',nd1) #保存文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[25, 62, 96,  6, 39],\n",
       "       [74, 49, 64, 75, 56],\n",
       "       [98, 85, 68, 11, 64]])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.load('./data.npy') #加载文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "#多个数据存到一个文件中\n",
    "np.savez('./data.npz',a = nd1,x = nd2) #a和x称为key，可以任意起名"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = np.load('./data.npz')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[25, 62, 96,  6, 39],\n",
       "       [74, 49, 64, 75, 56],\n",
       "       [98, 85, 68, 11, 64]])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data['a'] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.savez_compressed('./data2.npz',x = nd1,y = nd2) #保存时给压缩一下"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[25, 62, 96,  6, 39],\n",
       "       [74, 49, 64, 75, 56],\n",
       "       [98, 85, 68, 11, 64]])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.load('./data2.npz')['x']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.savetxt('./data.txt',#文件名\n",
    "           nd1,#数据\n",
    "           fmt = '%0.2f',#格式\n",
    "           delimiter = ';'#分隔符\n",
    "          )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.savetxt('./data.csv',#文件名\n",
    "           nd1,#数据\n",
    "           fmt = '%d',#格式\n",
    "           delimiter = ','#分隔符\n",
    "          )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[25., 62., 96.,  6., 39.],\n",
       "       [74., 49., 64., 75., 56.],\n",
       "       [98., 85., 68., 11., 64.]])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.loadtxt('./data.csv',delimiter=',')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[25., 62., 96.,  6., 39.],\n",
       "       [74., 49., 64., 75., 56.],\n",
       "       [98., 85., 68., 11., 64.]])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.loadtxt('./data.txt',delimiter=';')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "## 数据类型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hidden": true
   },
   "source": [
    "int: int8、uint8、int16、int32、int64\n",
    "float: float16、float32、float64\n",
    "str\n",
    "uint8无符号，表示256个数字，只有0-255\n",
    "int8,有符号，256个，-128~127"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 4, 7], dtype=int8)"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array([2,4,7],dtype = np.int8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([253, 249, 255, 108,   0,   0], dtype=uint8)"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array([-3,-7,255,108,0,256],dtype = np.uint8)#表示不了的自动按规则换成别的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([70, 34, 45, 82, 36,  8, 59, 65, 95, 94], dtype=int64)"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.randint(0,100,size = 10,dtype = 'int64')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.02932445, 0.66449089],\n",
       "       [0.74856845, 0.10077122],\n",
       "       [0.57512571, 0.42514385],\n",
       "       [0.71782876, 0.03539496],\n",
       "       [0.54101883, 0.43809984],\n",
       "       [0.78861366, 0.27951279],\n",
       "       [0.69234928, 0.79352346],\n",
       "       [0.65730027, 0.02961486],\n",
       "       [0.7761938 , 0.11304389],\n",
       "       [0.3321465 , 0.40396951]])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd = np.random.rand(10,2)\n",
    "nd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('float64')"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd.dtype #默认数据类型是float64！！！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.02933, 0.6646 ],\n",
       "       [0.7485 , 0.10077],\n",
       "       [0.575  , 0.425  ],\n",
       "       [0.718  , 0.0354 ],\n",
       "       [0.541  , 0.438  ],\n",
       "       [0.7886 , 0.2795 ],\n",
       "       [0.6924 , 0.7935 ],\n",
       "       [0.657  , 0.02962],\n",
       "       [0.7764 , 0.11304],\n",
       "       [0.332  , 0.404  ]], dtype=float16)"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.asarray(nd,dtype='float16')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.02933, 0.6646 ],\n",
       "       [0.7485 , 0.10077],\n",
       "       [0.575  , 0.425  ],\n",
       "       [0.718  , 0.0354 ],\n",
       "       [0.541  , 0.438  ],\n",
       "       [0.7886 , 0.2795 ],\n",
       "       [0.6924 , 0.7935 ],\n",
       "       [0.657  , 0.02962],\n",
       "       [0.7764 , 0.11304],\n",
       "       [0.332  , 0.404  ]], dtype=float16)"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd.astype(dtype = np.float16)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "#尤其当数据量大的时候，一定考虑数据类型,可如此进行设定\n",
    "np.save('./data2',nd.astype('float16'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数组运算"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 基本运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([6, 9, 2, 5, 8])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([0, 4, 1, 6, 7])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#加减乘除指数幂运算\n",
    "nd1 = np.random.randint(0,10,size = 5)\n",
    "nd2 = np.random.randint(0,10,size = 5)\n",
    "display(nd1,nd2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 6,  5,  1, -1,  1])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1-nd2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0, 36,  2, 30, 56])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1*nd2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([      1,    6561,       2,   15625, 2097152], dtype=int32)"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1**nd2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.power(2,3)  #如此，也可表示2的三次幂"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([      1,    6561,       2,   15625, 2097152], dtype=int32)"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.power(nd1,nd2)  #如此，表示对应位置次幂"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4.605170185988092"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.log(100)#默认底数是自然底数e"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10.0"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.log2(1024) #底数是2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 逻辑运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([6, 9, 2, 5, 8])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([0, 4, 1, 6, 7])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(nd1,nd2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True,  True,  True, False,  True])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1 >= nd2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False, False, False,  True, False])"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1 <nd2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False, False, False, False, False])"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1 == nd2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数组与标量计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([16, 19, 12, 15, 18])"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1+10 #-------------此为广播！！！！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.16666667, 0.11111111, 0.5       , 0.2       , 0.125     ])"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#数组还可以做分母，但不能有0\n",
    "1/nd1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### += -= /= 直接改变原数组(不支持/=)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([6, 9, 2, 5, 8])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([0, 4, 1, 6, 7])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(nd1,nd2) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [],
   "source": [
    "nd1+=100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([206, 209, 202, 205, 208])"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 复制和视图"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 完全没有复制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [],
   "source": [
    "a =np.random.randint(0,10,size =5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [],
   "source": [
    "b = a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a is b #证明赋值操作，a和b是一回事，改变a，则b也发生变化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([1024,    3,    4,    7,    2]), array([1024,    3,    4,    7,    2]))"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[0] = 1024\n",
    "a,b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 视图/查看/浅拷贝"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([0, 9, 5, 3, 9]), array([0, 9, 5, 3, 9]))"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a =np.random.randint(0,10,size =5)\n",
    "b = a.view() #视图，查看，浅拷贝\n",
    "a,b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a is b  #证明a和b不一样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.flags.owndata  #结果为true，证明a数组数据是自己的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.flags.owndata #结果为false，证明b数组不拥有自己的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([1024, 2028,    5,    3,    9]), array([1024, 2028,    5,    3,    9]))"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[0] = 1024\n",
    "b[1] = 2028   #无论修改谁，最终结果两个数组都发生变化\n",
    "a,b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 深拷贝"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([-32, -31, -43, -17, -59, -46, -57, -82, -28, -36]),\n",
       " array([-32, -31, -43, -17, -59, -46, -57, -82, -28, -36]))"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.randint(-100,0,size = 10)\n",
    "b = a.copy()  #深拷贝，此时，a和b没有关系了\n",
    "a,b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(a is b)\n",
    "display(a .flags.owndata)\n",
    "display(b.flags.owndata)  #对象也拥有了自己的数据，从a拷贝了一粉自己的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([1024,  -31,  -43,  -17,  -59,  -46,  -57,  -82,  -28,  -36]),\n",
       " array([ -32,  -31, 2048,  -17,  -59,  -46,  -57,  -82,  -28,  -36]))"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[0] = 1024\n",
    "b[2] =2048  #井水不犯河水，各人改各人的数据\n",
    "a,b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.0000000e+00, 1.0000000e+00, 2.0000000e+00, ..., 9.9999997e+07,\n",
       "       9.9999998e+07, 9.9999999e+07])"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#应用\n",
    "a = np.arange(1e8) #0~1亿，数据量非常多\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [],
   "source": [
    "b = a[[1,3,5,7,8,99]].copy() #取出一部分数据，原来的a数组就不用了，但占用内存，\n",
    "del a #将原来数据a删除，内存优化，此时直接操作b即可，a不存在了"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 索引、切片、迭代"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 基本索引和切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([18, 17, 23, 10, 20, 15, 23, 18, 23, 25])"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.randint(0,30,size=10)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([17, 10, 15])"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[0] #取1个\n",
    "a[[1,3,5]] #取多个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[13, 10,  8, 19, 10,  8, 29, 17, 21, 12],\n",
       "       [27, 13, 17,  9,  1, 23, 11, 16, 19, 15],\n",
       "       [28, 21,  0,  2, 17,  9, 11, 28, 17,  5],\n",
       "       [17, 21,  0, 13, 21, 12, 23,  4,  3, 20],\n",
       "       [13, 16, 15,  5,  2,  3,  0, 21,  8,  7],\n",
       "       [23, 23, 17,  0, 16, 29,  6, 20, 17, 27],\n",
       "       [12, 26,  2,  2, 11, 19, 16, 24, 20, 19],\n",
       "       [ 5, 23, 28, 11, 29, 28, 26, 22,  4, 24],\n",
       "       [10,  2, 14, 29, 16, 12, 24,  6, 28, 10],\n",
       "       [ 1, 10, 20,  0, 29, 27, 11, 26, 22,  3]])"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.random.randint(0,30,size = (10,10))\n",
    "b  #二维数组，索引和切片同理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([27, 13, 17,  9,  1, 23, 11, 16, 19, 15])"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[1] #第一行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[13, 10,  8, 19, 10,  8, 29, 17, 21, 12],\n",
       "       [17, 21,  0, 13, 21, 12, 23,  4,  3, 20],\n",
       "       [23, 23, 17,  0, 16, 29,  6, 20, 17, 27]])"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[[0,3,5]]  #第几行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "11"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[1,6] #第一行，第六列    ；  先行后列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0, 12, 23])"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[3,[2,5,6]]  #多维数组，第三行，第2,5,6列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[21, 17, 28],\n",
       "       [10, 29, 26]])"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[2::7,1::3]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 花式索引和索引技巧"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.arange(20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [],
   "source": [
    "b = a[3:7]#切片时，返回的数据，不是深拷贝"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([   0,    1,    2, 1024,    4,    5,    6,    7,    8,    9,   10,\n",
       "          11,   12,   13,   14,   15,   16,   17,   18,   19]),\n",
       " array([1024,    4,    5,    6]))"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[0] = 1024\n",
    "a,b   #a和b均发生变化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,\n",
       "       17, 18, 19])"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(20)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [],
   "source": [
    "b = a[[3,4,5,6]] #花式索引：索引时，给了一个数组\n",
    "#花式索引，返回的是深拷贝的数据！！！！！！！！！！！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,\n",
       "        17, 18, 19]),\n",
       " array([1024,    4,    5,    6]))"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[0] = 1024\n",
    "a,b  #此时，改变b后，a并未发生改变"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 299,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 40,  34,  67],\n",
       "       [145,   3,  58],\n",
       "       [148, 144,  89],\n",
       "       [ 76,  16,  53],\n",
       "       [150,  15,  35],\n",
       "       [ 27,  78,  89],\n",
       "       [ 44,   6,  87],\n",
       "       [ 75,  18, 137],\n",
       "       [135,  57,  84],\n",
       "       [ 29,  74,  21],\n",
       "       [ 48,   2, 119],\n",
       "       [ 15,  52,  82],\n",
       "       [125, 137,  17],\n",
       "       [  6,  36,  45],\n",
       "       [ 47, 135, 115],\n",
       "       [ 82,  23,  31],\n",
       "       [ 51,  88, 129],\n",
       "       [ 15,   4,  87],\n",
       "       [ 35,  72, 110],\n",
       "       [149,  97,  90],\n",
       "       [ 51,  24, 137],\n",
       "       [145, 125,  11],\n",
       "       [ 26, 120,  63],\n",
       "       [102,  97,  73],\n",
       "       [135,  17,   6],\n",
       "       [115,  43, 136],\n",
       "       [139, 115,  33],\n",
       "       [135, 149,  83],\n",
       "       [ 11, 140,  15],\n",
       "       [ 43, 149,  48],\n",
       "       [ 95,  96, 105],\n",
       "       [137,  93,  82],\n",
       "       [129,  47,  21],\n",
       "       [130,  25,  64],\n",
       "       [121, 123,  63],\n",
       "       [122,  97, 144],\n",
       "       [ 30,  18,  51],\n",
       "       [  3,  64,  74],\n",
       "       [ 70,  24, 138],\n",
       "       [112,  50,  73],\n",
       "       [ 70,  81, 137],\n",
       "       [ 68,  35, 104],\n",
       "       [128,  39,  60],\n",
       "       [ 64,   7,  96],\n",
       "       [ 71,  65,  11],\n",
       "       [146,  75,  87],\n",
       "       [146,  40,   8],\n",
       "       [137,  91,  59],\n",
       "       [134,  30,  28],\n",
       "       [ 33, 143,  89],\n",
       "       [ 36, 149, 145],\n",
       "       [ 98, 121,  24],\n",
       "       [122, 109, 126],\n",
       "       [107, 100,  17],\n",
       "       [ 48,  96,  30],\n",
       "       [ 85,  26,  40],\n",
       "       [ 44, 124,  21],\n",
       "       [ 51, 134,   8],\n",
       "       [112,  80, 115],\n",
       "       [ 51,  23, 149],\n",
       "       [ 83,  99, 126],\n",
       "       [ 56,  48,  11],\n",
       "       [ 87,  89,  18],\n",
       "       [ 78, 146,  36],\n",
       "       [ 75, 127, 134],\n",
       "       [ 20,  37,  88],\n",
       "       [131,  95,  78],\n",
       "       [ 55,  10,  51],\n",
       "       [ 28,  71,  42],\n",
       "       [ 60,  38,  97],\n",
       "       [ 71,  76, 150],\n",
       "       [ 95,   9,  57],\n",
       "       [107, 142,  27],\n",
       "       [ 43,  96,  83],\n",
       "       [146, 105,  32],\n",
       "       [104, 139, 115],\n",
       "       [  8,   8,  54],\n",
       "       [ 42, 136,  91],\n",
       "       [ 83,  54,  61],\n",
       "       [ 27, 147,  34],\n",
       "       [ 64,  94,  31],\n",
       "       [114, 127,  34],\n",
       "       [117, 116,  65],\n",
       "       [145,  65,  62],\n",
       "       [ 41, 140,  28],\n",
       "       [128, 148,   9],\n",
       "       [ 62, 138,  68],\n",
       "       [ 22,  82, 103],\n",
       "       [120,  31,  53],\n",
       "       [136, 146,  63],\n",
       "       [ 93, 125, 100],\n",
       "       [ 72,   5,  72],\n",
       "       [ 71,  55, 150],\n",
       "       [ 17,  85,  70],\n",
       "       [ 53,  46, 106],\n",
       "       [117,  13, 115],\n",
       "       [ 15, 127, 145],\n",
       "       [ 83,  94,  68],\n",
       "       [138, 125,  58],\n",
       "       [ 76,   2,  46]])"
      ]
     },
     "execution_count": 299,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.randint(0,151,size = (100,3)) #可理解为，100个学生，参加3门考试\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {},
   "outputs": [],
   "source": [
    "cond = a>=120  #逻辑运算\n",
    "cond"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([124, 135, 140, 133, 127, 135, 133, 145, 139, 123, 148, 132, 135,\n",
       "       130, 144, 134, 140, 123, 135, 124, 143, 138, 135, 121, 147, 147,\n",
       "       131, 144, 129, 122, 123, 124, 125, 142, 134, 143, 130, 150, 120,\n",
       "       128, 143, 144, 137, 141, 123, 125, 128, 124, 128, 132, 143, 135,\n",
       "       125, 142, 135, 129, 144, 132, 134, 136, 150, 148, 142, 128, 120,\n",
       "       126, 127])"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[cond]  #取出所有满足cond的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[124, 128, 132]])"
      ]
     },
     "execution_count": 123,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#要返回三门都大于120的\n",
    "#true = 1，false = 0.所以只要有一门是false，最终就是0\n",
    "#三个科目条件进行相乘，作为新条件\n",
    "cond2 = cond[:,0]*cond[:,1]*cond[:,2]\n",
    "a[cond2] #-----所以最终筛选结果为三门都大于120的同学"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[28, 20, 11]])"
      ]
     },
     "execution_count": 125,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#找到>=120,<=30\n",
    "cond1 = a>=120\n",
    "cond2 = a<= 30\n",
    "a[cond2[:,0]*cond2[:,1]*cond2[:,2]]#---->找到三门都小于30的同学"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 6, 1, 5, 9],\n",
       "       [3, 7, 7, 7, 0],\n",
       "       [3, 4, 4, 1, 1]])"
      ]
     },
     "execution_count": 129,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.randint(0,10,size=(3,5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[126,  87,  90],\n",
       "       [ 66, 146,  99],\n",
       "       [145,  94, 112],\n",
       "       [137, 133, 103],\n",
       "       [112, 113, 118],\n",
       "       [130, 126,  86],\n",
       "       [ 76,  73, 122],\n",
       "       [130,  75,  71],\n",
       "       [138, 118,  99],\n",
       "       [ 52, 130, 107],\n",
       "       [114, 141,  95],\n",
       "       [142,   4, 127],\n",
       "       [146, 130,  69],\n",
       "       [ 71, 101, 138],\n",
       "       [107,  94, 106],\n",
       "       [125, 115,  45],\n",
       "       [ 91, 136,  83],\n",
       "       [ 91, 147,  78],\n",
       "       [104,  99,  99],\n",
       "       [126,  86,  97],\n",
       "       [107, 110,  91],\n",
       "       [148, 141,  75],\n",
       "       [132, 143,  71],\n",
       "       [ 64, 116, 113],\n",
       "       [ 70, 130, 104],\n",
       "       [143,  78, 133]])"
      ]
     },
     "execution_count": 136,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xx = np.random.randint(0,151,size=(100,3))\n",
    "cond = xx.mean(axis = 1)>90\n",
    "xx[cond]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "## 形状操作"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hidden": true
   },
   "source": [
    "### 数组变形"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5, 6, 6, 9, 2],\n",
       "       [2, 7, 6, 4, 6],\n",
       "       [2, 3, 9, 9, 7]])"
      ]
     },
     "execution_count": 139,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.randint(0,10,size=(3,5))\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5, 6, 6],\n",
       "       [9, 2, 2],\n",
       "       [7, 6, 4],\n",
       "       [6, 2, 3],\n",
       "       [9, 9, 7]])"
      ]
     },
     "execution_count": 140,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.reshape(5,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5, 6, 6],\n",
       "       [9, 2, 2],\n",
       "       [7, 6, 4],\n",
       "       [6, 2, 3],\n",
       "       [9, 9, 7]])"
      ]
     },
     "execution_count": 141,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.reshape(-1,3) #自动整形！！！！！-1表示数据，3自动计算------> -1 =5"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hidden": true
   },
   "source": [
    "### 数组转置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5, 2, 2],\n",
       "       [6, 7, 3],\n",
       "       [6, 6, 9],\n",
       "       [9, 4, 9],\n",
       "       [2, 6, 7]])"
      ]
     },
     "execution_count": 142,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 144,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[9, 2, 6, 3, 4, 3, 4],\n",
       "        [2, 2, 5, 1, 0, 4, 0],\n",
       "        [7, 9, 5, 8, 2, 6, 5],\n",
       "        [0, 6, 4, 2, 3, 2, 5],\n",
       "        [0, 1, 2, 0, 4, 2, 9]],\n",
       "\n",
       "       [[4, 0, 6, 7, 6, 3, 4],\n",
       "        [0, 0, 9, 7, 7, 4, 3],\n",
       "        [2, 9, 0, 1, 8, 3, 7],\n",
       "        [0, 6, 1, 0, 5, 8, 7],\n",
       "        [3, 1, 4, 2, 6, 8, 5]],\n",
       "\n",
       "       [[6, 7, 2, 0, 8, 0, 2],\n",
       "        [3, 7, 8, 4, 9, 4, 3],\n",
       "        [9, 3, 3, 1, 3, 6, 8],\n",
       "        [8, 2, 1, 0, 2, 3, 6],\n",
       "        [5, 5, 8, 9, 5, 7, 8]]])"
      ]
     },
     "execution_count": 144,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#三维数组可通过transpose进行对调\n",
    "b = np.random.randint(0,10,size = (3,5,7))  #shape = (0,1,2)\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 148,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(7, 5, 3)"
      ]
     },
     "execution_count": 148,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = np.transpose(b,(2,1,0)) #调整维度结构，2和0维度数据对调\n",
    "c.shape  #------------->变成（7,5,3）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 149,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[9, 4, 6],\n",
       "        [2, 0, 3],\n",
       "        [7, 2, 9],\n",
       "        [0, 0, 8],\n",
       "        [0, 3, 5]],\n",
       "\n",
       "       [[2, 0, 7],\n",
       "        [2, 0, 7],\n",
       "        [9, 9, 3],\n",
       "        [6, 6, 2],\n",
       "        [1, 1, 5]],\n",
       "\n",
       "       [[6, 6, 2],\n",
       "        [5, 9, 8],\n",
       "        [5, 0, 3],\n",
       "        [4, 1, 1],\n",
       "        [2, 4, 8]],\n",
       "\n",
       "       [[3, 7, 0],\n",
       "        [1, 7, 4],\n",
       "        [8, 1, 1],\n",
       "        [2, 0, 0],\n",
       "        [0, 2, 9]],\n",
       "\n",
       "       [[4, 6, 8],\n",
       "        [0, 7, 9],\n",
       "        [2, 8, 3],\n",
       "        [3, 5, 2],\n",
       "        [4, 6, 5]],\n",
       "\n",
       "       [[3, 3, 0],\n",
       "        [4, 4, 4],\n",
       "        [6, 3, 6],\n",
       "        [2, 8, 3],\n",
       "        [2, 8, 7]],\n",
       "\n",
       "       [[4, 4, 2],\n",
       "        [0, 3, 3],\n",
       "        [5, 7, 8],\n",
       "        [5, 7, 6],\n",
       "        [9, 5, 8]]])"
      ]
     },
     "execution_count": 149,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hidden": true
   },
   "source": [
    "### 数据堆叠合并"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 150,
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "nd1 = np.random.randint(0,10,size = (3,5))\n",
    "nd2 = np.random.randint(0,10,size = (3,5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 151,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 7, 2, 2, 9],\n",
       "       [0, 3, 4, 3, 1],\n",
       "       [2, 3, 2, 7, 8],\n",
       "       [7, 6, 2, 3, 6],\n",
       "       [8, 2, 0, 3, 2],\n",
       "       [3, 0, 3, 5, 4]])"
      ]
     },
     "execution_count": 151,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.concatenate([nd1,nd2])#默认合并行增加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 152,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 7, 2, 2, 9, 7, 6, 2, 3, 6],\n",
       "       [0, 3, 4, 3, 1, 8, 2, 0, 3, 2],\n",
       "       [2, 3, 2, 7, 8, 3, 0, 3, 5, 4]])"
      ]
     },
     "execution_count": 152,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.concatenate([nd1,nd2],axis = 1)#此时列增加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 154,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 7, 2, 2, 9, 7, 6, 2, 3, 6],\n",
       "       [0, 3, 4, 3, 1, 8, 2, 0, 3, 2],\n",
       "       [2, 3, 2, 7, 8, 3, 0, 3, 5, 4]])"
      ]
     },
     "execution_count": 154,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.hstack([nd1,nd2])#堆叠，合并，h表示水平，即列增多"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 155,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 7, 2, 2, 9],\n",
       "       [0, 3, 4, 3, 1],\n",
       "       [2, 3, 2, 7, 8],\n",
       "       [7, 6, 2, 3, 6],\n",
       "       [8, 2, 0, 3, 2],\n",
       "       [3, 0, 3, 5, 4]])"
      ]
     },
     "execution_count": 155,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vstack([nd1,nd2])#v表示竖直，行增加"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hidden": true
   },
   "source": [
    "### 数组拆分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 157,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[78, 42, 82, 85,  1, 77, 27,  5, 80, 93],\n",
       "       [87, 14, 45,  6, 78, 81, 56, 39, 18, 36],\n",
       "       [29, 21, 20, 29,  8,  3, 27, 49, 37, 13],\n",
       "       [ 8,  6, 91, 55, 60, 82, 27, 82,  2, 92],\n",
       "       [79, 30, 38, 66, 89, 15, 74, 79, 79,  7],\n",
       "       [ 5, 43, 15,  4, 42, 27, 53, 29,  8, 66],\n",
       "       [88, 23, 44,  0, 98, 30, 91, 58, 36, 42],\n",
       "       [34, 12, 12,  5, 24, 55, 19, 67, 19, 82],\n",
       "       [62, 96, 41, 67, 36, 30, 49, 19, 47, 87],\n",
       "       [12, 99, 13, 81, 82, 58, 68,  0, 72, 15],\n",
       "       [29, 26, 24, 13,  7, 10, 77, 21, 41, 44],\n",
       "       [28, 16, 81,  0, 43, 75, 10, 65, 76,  4],\n",
       "       [14, 39, 43, 49, 96, 71, 40, 78, 59, 82],\n",
       "       [23, 29, 77, 11,  0,  2, 19, 10, 17, 49],\n",
       "       [48, 20, 60, 59, 14, 50, 25, 83, 54, 93]])"
      ]
     },
     "execution_count": 157,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.randint(0,100,size = (15,10))\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 158,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[78, 42, 82, 85,  1, 77, 27,  5, 80, 93],\n",
       "        [87, 14, 45,  6, 78, 81, 56, 39, 18, 36],\n",
       "        [29, 21, 20, 29,  8,  3, 27, 49, 37, 13],\n",
       "        [ 8,  6, 91, 55, 60, 82, 27, 82,  2, 92],\n",
       "        [79, 30, 38, 66, 89, 15, 74, 79, 79,  7]]),\n",
       " array([[ 5, 43, 15,  4, 42, 27, 53, 29,  8, 66],\n",
       "        [88, 23, 44,  0, 98, 30, 91, 58, 36, 42],\n",
       "        [34, 12, 12,  5, 24, 55, 19, 67, 19, 82],\n",
       "        [62, 96, 41, 67, 36, 30, 49, 19, 47, 87],\n",
       "        [12, 99, 13, 81, 82, 58, 68,  0, 72, 15]]),\n",
       " array([[29, 26, 24, 13,  7, 10, 77, 21, 41, 44],\n",
       "        [28, 16, 81,  0, 43, 75, 10, 65, 76,  4],\n",
       "        [14, 39, 43, 49, 96, 71, 40, 78, 59, 82],\n",
       "        [23, 29, 77, 11,  0,  2, 19, 10, 17, 49],\n",
       "        [48, 20, 60, 59, 14, 50, 25, 83, 54, 93]])]"
      ]
     },
     "execution_count": 158,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.split(a,indices_or_sections=3)#平均分成3份，"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 159,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[78, 42, 82, 85,  1],\n",
       "        [87, 14, 45,  6, 78],\n",
       "        [29, 21, 20, 29,  8],\n",
       "        [ 8,  6, 91, 55, 60],\n",
       "        [79, 30, 38, 66, 89],\n",
       "        [ 5, 43, 15,  4, 42],\n",
       "        [88, 23, 44,  0, 98],\n",
       "        [34, 12, 12,  5, 24],\n",
       "        [62, 96, 41, 67, 36],\n",
       "        [12, 99, 13, 81, 82],\n",
       "        [29, 26, 24, 13,  7],\n",
       "        [28, 16, 81,  0, 43],\n",
       "        [14, 39, 43, 49, 96],\n",
       "        [23, 29, 77, 11,  0],\n",
       "        [48, 20, 60, 59, 14]]),\n",
       " array([[77, 27,  5, 80, 93],\n",
       "        [81, 56, 39, 18, 36],\n",
       "        [ 3, 27, 49, 37, 13],\n",
       "        [82, 27, 82,  2, 92],\n",
       "        [15, 74, 79, 79,  7],\n",
       "        [27, 53, 29,  8, 66],\n",
       "        [30, 91, 58, 36, 42],\n",
       "        [55, 19, 67, 19, 82],\n",
       "        [30, 49, 19, 47, 87],\n",
       "        [58, 68,  0, 72, 15],\n",
       "        [10, 77, 21, 41, 44],\n",
       "        [75, 10, 65, 76,  4],\n",
       "        [71, 40, 78, 59, 82],\n",
       "        [ 2, 19, 10, 17, 49],\n",
       "        [50, 25, 83, 54, 93]])]"
      ]
     },
     "execution_count": 159,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.split(a,indices_or_sections=2.,axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 160,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[78, 42, 82, 85,  1, 77, 27,  5, 80, 93]]),\n",
       " array([[87, 14, 45,  6, 78, 81, 56, 39, 18, 36],\n",
       "        [29, 21, 20, 29,  8,  3, 27, 49, 37, 13],\n",
       "        [ 8,  6, 91, 55, 60, 82, 27, 82,  2, 92],\n",
       "        [79, 30, 38, 66, 89, 15, 74, 79, 79,  7]]),\n",
       " array([[ 5, 43, 15,  4, 42, 27, 53, 29,  8, 66],\n",
       "        [88, 23, 44,  0, 98, 30, 91, 58, 36, 42],\n",
       "        [34, 12, 12,  5, 24, 55, 19, 67, 19, 82],\n",
       "        [62, 96, 41, 67, 36, 30, 49, 19, 47, 87]]),\n",
       " array([[12, 99, 13, 81, 82, 58, 68,  0, 72, 15],\n",
       "        [29, 26, 24, 13,  7, 10, 77, 21, 41, 44],\n",
       "        [28, 16, 81,  0, 43, 75, 10, 65, 76,  4],\n",
       "        [14, 39, 43, 49, 96, 71, 40, 78, 59, 82],\n",
       "        [23, 29, 77, 11,  0,  2, 19, 10, 17, 49],\n",
       "        [48, 20, 60, 59, 14, 50, 25, 83, 54, 93]])]"
      ]
     },
     "execution_count": 160,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.split(a,indices_or_sections=[1,5,9])#0-1.1-5,5-9,9-"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 162,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[78, 42, 82, 85,  1],\n",
       "        [87, 14, 45,  6, 78],\n",
       "        [29, 21, 20, 29,  8],\n",
       "        [ 8,  6, 91, 55, 60],\n",
       "        [79, 30, 38, 66, 89],\n",
       "        [ 5, 43, 15,  4, 42],\n",
       "        [88, 23, 44,  0, 98],\n",
       "        [34, 12, 12,  5, 24],\n",
       "        [62, 96, 41, 67, 36],\n",
       "        [12, 99, 13, 81, 82],\n",
       "        [29, 26, 24, 13,  7],\n",
       "        [28, 16, 81,  0, 43],\n",
       "        [14, 39, 43, 49, 96],\n",
       "        [23, 29, 77, 11,  0],\n",
       "        [48, 20, 60, 59, 14]]),\n",
       " array([[77, 27,  5, 80, 93],\n",
       "        [81, 56, 39, 18, 36],\n",
       "        [ 3, 27, 49, 37, 13],\n",
       "        [82, 27, 82,  2, 92],\n",
       "        [15, 74, 79, 79,  7],\n",
       "        [27, 53, 29,  8, 66],\n",
       "        [30, 91, 58, 36, 42],\n",
       "        [55, 19, 67, 19, 82],\n",
       "        [30, 49, 19, 47, 87],\n",
       "        [58, 68,  0, 72, 15],\n",
       "        [10, 77, 21, 41, 44],\n",
       "        [75, 10, 65, 76,  4],\n",
       "        [71, 40, 78, 59, 82],\n",
       "        [ 2, 19, 10, 17, 49],\n",
       "        [50, 25, 83, 54, 93]])]"
      ]
     },
     "execution_count": 162,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.hsplit(a,indices_or_sections=2)#h表示水平切2份\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 166,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[78, 42, 82, 85,  1, 77, 27,  5, 80, 93],\n",
       "        [87, 14, 45,  6, 78, 81, 56, 39, 18, 36],\n",
       "        [29, 21, 20, 29,  8,  3, 27, 49, 37, 13],\n",
       "        [ 8,  6, 91, 55, 60, 82, 27, 82,  2, 92],\n",
       "        [79, 30, 38, 66, 89, 15, 74, 79, 79,  7]]),\n",
       " array([[ 5, 43, 15,  4, 42, 27, 53, 29,  8, 66],\n",
       "        [88, 23, 44,  0, 98, 30, 91, 58, 36, 42],\n",
       "        [34, 12, 12,  5, 24, 55, 19, 67, 19, 82],\n",
       "        [62, 96, 41, 67, 36, 30, 49, 19, 47, 87],\n",
       "        [12, 99, 13, 81, 82, 58, 68,  0, 72, 15]]),\n",
       " array([[29, 26, 24, 13,  7, 10, 77, 21, 41, 44],\n",
       "        [28, 16, 81,  0, 43, 75, 10, 65, 76,  4],\n",
       "        [14, 39, 43, 49, 96, 71, 40, 78, 59, 82],\n",
       "        [23, 29, 77, 11,  0,  2, 19, 10, 17, 49],\n",
       "        [48, 20, 60, 59, 14, 50, 25, 83, 54, 93]])]"
      ]
     },
     "execution_count": 166,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vsplit(a,indices_or_sections=3)#h表示竖直切3份"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "## 广播机制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 169,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3])"
      ]
     },
     "execution_count": 169,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 = np.array([0,1,2,3]*3)\n",
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 180,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0],\n",
       "       [1, 1, 1],\n",
       "       [2, 2, 2],\n",
       "       [3, 3, 3]])"
      ]
     },
     "execution_count": 180,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1.sort()\n",
    "arr1 = arr1.reshape(4,3)\n",
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 179,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3])"
      ]
     },
     "execution_count": 179,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2 = np.array([1,2,3])\n",
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 181,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [2, 3, 4],\n",
       "       [3, 4, 5],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 181,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 + arr2   #arr1和arr2形状不一样，但可以进行运算，是因为numpy底层为我们进行了广播\n",
    "# 每一行都进行相加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 183,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1],\n",
       "       [2],\n",
       "       [3],\n",
       "       [4]])"
      ]
     },
     "execution_count": 183,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr3 = np.array([[1],[2],[3],[4]])\n",
    "arr3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 186,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1, 1],\n",
       "       [3, 3, 3],\n",
       "       [5, 5, 5],\n",
       "       [7, 7, 7]])"
      ]
     },
     "execution_count": 186,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 + arr3  #没一列进行相加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 191,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[0, 1],\n",
       "        [2, 3],\n",
       "        [4, 5],\n",
       "        [6, 7]],\n",
       "\n",
       "       [[0, 1],\n",
       "        [2, 3],\n",
       "        [4, 5],\n",
       "        [6, 7]],\n",
       "\n",
       "       [[0, 1],\n",
       "        [2, 3],\n",
       "        [4, 5],\n",
       "        [6, 7]]])"
      ]
     },
     "execution_count": 191,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#三维数组怎么广播呢？\n",
    "a = np.array([0,1,2,3,4,5,6,7]*3).reshape(3,4,2)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 193,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1],\n",
       "       [2, 3],\n",
       "       [4, 5],\n",
       "       [6, 7]])"
      ]
     },
     "execution_count": 193,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.array([0,1,2,3,4,5,6,7]).reshape(4,2)\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 195,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 0,  2],\n",
       "        [ 4,  6],\n",
       "        [ 8, 10],\n",
       "        [12, 14]],\n",
       "\n",
       "       [[ 0,  2],\n",
       "        [ 4,  6],\n",
       "        [ 8, 10],\n",
       "        [12, 14]],\n",
       "\n",
       "       [[ 0,  2],\n",
       "        [ 4,  6],\n",
       "        [ 8, 10],\n",
       "        [12, 14]]])"
      ]
     },
     "execution_count": 195,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a +b   #b广播了三份"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 通用函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 元素级、数字级的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 196,
   "metadata": {},
   "outputs": [],
   "source": [
    "#abs、sqrt(开平方)、square（平方）\n",
    "#exp、log、sin、cos、tan\n",
    "#maxinmum、minimum\n",
    "#all、any、inner、clip、round、trace、ceil、floor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 209,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  4,  8,  9, 16, 25])"
      ]
     },
     "execution_count": 209,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 = np.array([-1,4,8,-9,16,25])\n",
    "arr2 = np.random.randint(1,5,size = 6)\n",
    "np.abs(arr1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 202,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "20.085536923187668"
      ]
     },
     "execution_count": 202,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.exp(3)#自然底数e的多少次幂 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 201,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "20.085536923187664"
      ]
     },
     "execution_count": 201,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.e**3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 203,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.9999999540371913"
      ]
     },
     "execution_count": 203,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.log(20.085536)\n",
    "np.sin(np.pi/2) #---------90度\n",
    "np.tan(np.pi/6)#------30度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 205,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([-1,  4,  8, -9, 16, 25]), array([1, 4, 3, 1, 3, 2, 4]))"
      ]
     },
     "execution_count": 205,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1,arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 210,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 3,  4,  8,  3, 16, 25])"
      ]
     },
     "execution_count": 210,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.maximum(arr1,arr2)   #----从arr1和arr2中分别选最大的那个，组成一个新的array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 211,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-1,  1,  2, -9,  2,  4])"
      ]
     },
     "execution_count": 211,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.minimum(arr1,arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 212,
   "metadata": {},
   "outputs": [],
   "source": [
    "nd1 = np.array([1,3,0])  #出现0那么对应false"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 213,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 213,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1.any() #----有一个true就是true"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 214,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 214,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1.all()#----所有都是true才是true"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 215,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "60"
      ]
     },
     "execution_count": 215,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([1,2,3,4,5])\n",
    "b = np.array([1,2,3,4,6])\n",
    "np.inner(a,b) #返回内积"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 216,
   "metadata": {},
   "outputs": [],
   "source": [
    "nd = np.random.randint(1,100,size=30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 218,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([56, 14, 14, 50, 10, 14, 10, 48, 80, 80, 80, 10, 13, 16, 63, 80, 49,\n",
       "       68, 22, 72, 65, 24, 72, 76, 68, 54, 41, 48, 16, 23])"
      ]
     },
     "execution_count": 218,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.clip(nd,10,80) #把小于10和大于80的都裁剪掉"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 220,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3., 3., 3.])"
      ]
     },
     "execution_count": 220,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ceil(np.array([2.7,2.1,2.05])) #向上取整"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 222,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2., 2., 2.])"
      ]
     },
     "execution_count": 222,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.floor(np.array([2.7,2.1,2.05]))#向下取整"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 227,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 227,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.randint(0,10,size = (3,3))\n",
    "np.trace(a)#计算对角线上的和"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### where函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 231,
   "metadata": {},
   "outputs": [],
   "source": [
    "nd1 = np.array([1,3,5,7,9])\n",
    "nd2 = np.array([2,4,6,8,10])\n",
    "cond = np.array([True,False,False,True,True])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 232,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 4, 6, 7, 9])"
      ]
     },
     "execution_count": 232,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.where(cond,nd1,nd2)#条件如果是true返回nd1中数据，flase则返回nd2中数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 237,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  42,   93,   34,    9,   29,   43,   98,   64,   45,   22,    7,\n",
       "         82,   53, -100,   33,   22, -100,   14,   56,   78,   20,   44,\n",
       "         85,   76,   80,   88,   17,   75, -100,   34,   70,   61,   82,\n",
       "         81,   54,   27,   34,   25,   96,   55,    9,   74,    6,   53,\n",
       "       -100,   97, -100,   84,   35,    9])"
      ]
     },
     "execution_count": 237,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.randint(0,100,size=50)\n",
    "np.where(a>5,a,-100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 239,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([42, 93, 34,  9, 29, 43, 98, 64, 45, 22,  7, 82, 63,  4, 33, 22,  5,\n",
       "       14, 66, 78, 20, 44, 85, 76, 80, 88, 17, 75,  3, 34, 70, 61, 82, 81,\n",
       "       64, 27, 34, 25, 96, 65,  9, 74,  6, 63,  1, 97,  4, 84, 35,  9])"
      ]
     },
     "execution_count": 239,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cond1 = (a>=50) & (a<60)\n",
    "np.where(cond1,a+10,a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 240,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  0,  1,  6,  8,  9, 10, 11, 13, 14, 15, 15, 19, 21, 22, 26, 28,\n",
       "       31, 32, 34, 34, 34, 37, 39, 42, 44, 44, 45, 48, 53, 53, 56, 57, 58,\n",
       "       60, 63, 64, 65, 65, 67, 85, 85, 88, 89, 90, 93, 94, 94, 98, 98])"
      ]
     },
     "execution_count": 240,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.randint(0,100,size=50)\n",
    "np.sort(a)       #有打印输出，说明原数组没有改变，可以用一个变量b接收结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 242,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  0,  1,  6,  8,  9, 10, 11, 13, 14, 15, 15, 19, 21, 22, 26, 28,\n",
       "       31, 32, 34, 34, 34, 37, 39, 42, 44, 44, 45, 48, 53, 53, 56, 57, 58,\n",
       "       60, 63, 64, 65, 65, 67, 85, 85, 88, 89, 90, 93, 94, 94, 98, 98])"
      ]
     },
     "execution_count": 242,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.sort() #---------->没有打印输出，说明在原数组上进行了排序\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 247,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.random.randint(0,100,size=50)\n",
    "index = a.argsort()  #-----返回排序的索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 248,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  3,  3,  8, 11, 13, 18, 19, 19, 22, 22, 23, 23, 24, 25, 26,\n",
       "       26, 30, 32, 37, 37, 42, 45, 48, 48, 51, 54, 55, 58, 60, 63, 67, 68,\n",
       "       70, 71, 75, 76, 76, 79, 81, 81, 82, 83, 83, 91, 92, 93, 96, 98])"
      ]
     },
     "execution_count": 248,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#根据索引，花式索引\n",
    "a[index]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 249,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([98, 96, 93, 92, 91, 83, 83, 82, 81, 81, 79, 76, 76, 75, 71, 70, 68,\n",
       "       67, 63, 60, 58, 55, 54, 51, 48, 48, 45, 42, 37, 37, 32, 30, 26, 26,\n",
       "       25, 24, 23, 23, 22, 22, 19, 19, 18, 13, 11,  8,  3,  3,  1,  0])"
      ]
     },
     "execution_count": 249,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[index][::-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 集合操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 250,
   "metadata": {},
   "outputs": [],
   "source": [
    "A = np.array([2,4,6,8])\n",
    "B = np.array([3,4,5,6])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 251,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4, 6])"
      ]
     },
     "execution_count": 251,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.intersect1d(A,B) # 交集 array([4, 6])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 252,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 3, 4, 5, 6, 8])"
      ]
     },
     "execution_count": 252,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.union1d(A,B) # 并集 array([2, 3, 4, 5, 6, 8])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 253,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 8])"
      ]
     },
     "execution_count": 253,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.setdiff1d(A,B) #差集，A中有，B中没有 array([2, 8])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数学和统计函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 263,
   "metadata": {},
   "outputs": [],
   "source": [
    "# min、max\n",
    "# mean、median、sum、std、var、cumsum(累加)、cumprod（累乘）\n",
    "# argmin、argmax\n",
    "# argwhere、cov、corrcoef"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 258,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[95, 21, 83, 23, 69],\n",
       "       [69, 10, 34, 93, 43],\n",
       "       [35, 21, 82,  7,  5]])"
      ]
     },
     "execution_count": 258,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.randint(0,100,size=(3,5))\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 259,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 259,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.min()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 260,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([35, 10, 34,  7,  5])"
      ]
     },
     "execution_count": 260,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.min(axis = 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 261,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([21, 10,  5])"
      ]
     },
     "execution_count": 261,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.min(axis = 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 271,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[47, 87, 16,  8, 48],\n",
       "       [21, 85, 45, 83, 21],\n",
       "       [40, 54,  7,  9, 87]])"
      ]
     },
     "execution_count": 271,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 = np.random.randint(0,100,size=(3,5))\n",
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 269,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 269,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1.argmax() # 计算最⼤值的索引 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 278,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1],\n",
       "       [0, 2],\n",
       "       [0, 3],\n",
       "       [1, 1],\n",
       "       [1, 3],\n",
       "       [2, 1],\n",
       "       [2, 2],\n",
       "       [2, 3],\n",
       "       [2, 4]], dtype=int64)"
      ]
     },
     "execution_count": 278,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "index = np.argwhere((arr1 > 50) | (arr1<20)) # 返回⼤于20的元素的索引\n",
    "index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 280,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "21\n",
      "83\n",
      "23\n",
      "10\n",
      "93\n",
      "21\n",
      "82\n",
      "7\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "for i,j in index:\n",
    "    print(a[i,j])      #好！！！！！！！！！！！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 281,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 47, 134, 150, 158, 206, 227, 312, 357, 440, 461, 501, 555, 562,\n",
       "       571, 658], dtype=int32)"
      ]
     },
     "execution_count": 281,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.cumsum(arr1) # 计算累加和\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 282,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([6.2, 4.8, 3.8, 5.8])"
      ]
     },
     "execution_count": 282,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2 = np.random.randint(0,10,size = (4,5))\n",
    "arr2.mean(axis = 0) # 计算列的平均值\n",
    "arr2.mean(axis = 1) # 计算⾏的平均值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 283,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 6.7 ,  0.3 , -2.45, -4.2 ],\n",
       "       [ 0.3 , 11.7 ,  1.2 , -8.55],\n",
       "       [-2.45,  1.2 ,  6.7 ,  2.7 ],\n",
       "       [-4.2 , -8.55,  2.7 , 10.2 ]])"
      ]
     },
     "execution_count": 283,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.cov(arr2,rowvar=True) # 协⽅差矩阵\n",
    "#举例：一个男生受女生欢迎程度，和这名男生猥琐程度是否成正比?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 285,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.        ,  0.03388371, -0.36567164, -0.50805626],\n",
       "       [ 0.03388371,  1.        ,  0.13553483, -0.78265993],\n",
       "       [-0.36567164,  0.13553483,  1.        ,  0.32660759],\n",
       "       [-0.50805626, -0.78265993,  0.32660759,  1.        ]])"
      ]
     },
     "execution_count": 285,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.corrcoef(arr2,rowvar=True) # 相关性系数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 线性代数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 286,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 25,  23],\n",
       "       [ -4, -11]])"
      ]
     },
     "execution_count": 286,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#矩阵的乘积\n",
    "A = np.array([[4,2,3],\n",
    " [1,3,1]]) # shape(2,3)\n",
    "B = np.array([[2,7],\n",
    " [-5,-7],\n",
    " [9,3]]) # shape(3,2)\n",
    "np.dot(A,B) # 矩阵运算 A的最后⼀维和B的第⼀维必须⼀致\n",
    "A @ B # 符号 @ 表示矩阵乘积运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#计算矩阵的逆\n",
    "from numpy.linalg import inv,det,eig,qr,svd\n",
    "A = np.array([[1,2,3]\n",
    " [2,3,4],\n",
    " [4,5,8]]) # shape(3,3)\n",
    "inv(t) # 逆矩阵\n",
    "det(t)#计算矩阵⾏列式"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 鸢尾花实战"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 288,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = np.loadtxt('./iris.csv',delimiter = ',') # 读取数据⽂件，data是⼆维的数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 289,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5.1, 4.9, 4.7, 4.6, 5. , 5.4, 4.6, 5. , 4.4, 4.9, 5.4, 4.8, 4.8,\n",
       "       4.3, 5.8, 5.7, 5.4, 5.1, 5.7, 5.1, 5.4, 5.1, 4.6, 5.1, 4.8, 5. ,\n",
       "       5. , 5.2, 5.2, 4.7, 4.8, 5.4, 5.2, 5.5, 4.9, 5. , 5.5, 4.9, 4.4,\n",
       "       5.1, 5. , 4.5, 4.4, 5. , 5.1, 4.8, 5.1, 4.6, 5.3, 5. , 7. , 6.4,\n",
       "       6.9, 5.5, 6.5, 5.7, 6.3, 4.9, 6.6, 5.2, 5. , 5.9, 6. , 6.1, 5.6,\n",
       "       6.7, 5.6, 5.8, 6.2, 5.6, 5.9, 6.1, 6.3, 6.1, 6.4, 6.6, 6.8, 6.7,\n",
       "       6. , 5.7, 5.5, 5.5, 5.8, 6. , 5.4, 6. , 6.7, 6.3, 5.6, 5.5, 5.5,\n",
       "       6.1, 5.8, 5. , 5.6, 5.7, 5.7, 6.2, 5.1, 5.7, 6.3, 5.8, 7.1, 6.3,\n",
       "       6.5, 7.6, 4.9, 7.3, 6.7, 7.2, 6.5, 6.4, 6.8, 5.7, 5.8, 6.4, 6.5,\n",
       "       7.7, 7.7, 6. , 6.9, 5.6, 7.7, 6.3, 6.7, 7.2, 6.2, 6.1, 6.4, 7.2,\n",
       "       7.4, 7.9, 6.4, 6.3, 6.1, 7.7, 6.3, 6.4, 6. , 6.9, 6.7, 6.9, 5.8,\n",
       "       6.8, 6.7, 6.7, 6.3, 6.5, 6.2, 5.9])"
      ]
     },
     "execution_count": 289,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 291,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4.3, 4.4, 4.4, 4.4, 4.5, 4.6, 4.6, 4.6, 4.6, 4.7, 4.7, 4.8, 4.8,\n",
       "       4.8, 4.8, 4.8, 4.9, 4.9, 4.9, 4.9, 4.9, 4.9, 5. , 5. , 5. , 5. ,\n",
       "       5. , 5. , 5. , 5. , 5. , 5. , 5.1, 5.1, 5.1, 5.1, 5.1, 5.1, 5.1,\n",
       "       5.1, 5.1, 5.2, 5.2, 5.2, 5.2, 5.3, 5.4, 5.4, 5.4, 5.4, 5.4, 5.4,\n",
       "       5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.6, 5.6, 5.6, 5.6, 5.6, 5.6,\n",
       "       5.7, 5.7, 5.7, 5.7, 5.7, 5.7, 5.7, 5.7, 5.8, 5.8, 5.8, 5.8, 5.8,\n",
       "       5.8, 5.8, 5.9, 5.9, 5.9, 6. , 6. , 6. , 6. , 6. , 6. , 6.1, 6.1,\n",
       "       6.1, 6.1, 6.1, 6.1, 6.2, 6.2, 6.2, 6.2, 6.3, 6.3, 6.3, 6.3, 6.3,\n",
       "       6.3, 6.3, 6.3, 6.3, 6.4, 6.4, 6.4, 6.4, 6.4, 6.4, 6.4, 6.5, 6.5,\n",
       "       6.5, 6.5, 6.5, 6.6, 6.6, 6.7, 6.7, 6.7, 6.7, 6.7, 6.7, 6.7, 6.7,\n",
       "       6.8, 6.8, 6.8, 6.9, 6.9, 6.9, 6.9, 7. , 7.1, 7.2, 7.2, 7.2, 7.3,\n",
       "       7.4, 7.6, 7.7, 7.7, 7.7, 7.7, 7.9])"
      ]
     },
     "execution_count": 291,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.sort(axis = -1) # 简单排序\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 292,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 5. , 5.1, 5.2, 5.3, 5.4, 5.5,\n",
       "       5.6, 5.7, 5.8, 5.9, 6. , 6.1, 6.2, 6.3, 6.4, 6.5, 6.6, 6.7, 6.8,\n",
       "       6.9, 7. , 7.1, 7.2, 7.3, 7.4, 7.6, 7.7, 7.9])"
      ]
     },
     "execution_count": 292,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " np.unique(data) #去除重复数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 293,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "876.5"
      ]
     },
     "execution_count": 293,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 294,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  4.3,   8.7,  13.1,  17.5,  22. ,  26.6,  31.2,  35.8,  40.4,\n",
       "        45.1,  49.8,  54.6,  59.4,  64.2,  69. ,  73.8,  78.7,  83.6,\n",
       "        88.5,  93.4,  98.3, 103.2, 108.2, 113.2, 118.2, 123.2, 128.2,\n",
       "       133.2, 138.2, 143.2, 148.2, 153.2, 158.3, 163.4, 168.5, 173.6,\n",
       "       178.7, 183.8, 188.9, 194. , 199.1, 204.3, 209.5, 214.7, 219.9,\n",
       "       225.2, 230.6, 236. , 241.4, 246.8, 252.2, 257.6, 263.1, 268.6,\n",
       "       274.1, 279.6, 285.1, 290.6, 296.1, 301.7, 307.3, 312.9, 318.5,\n",
       "       324.1, 329.7, 335.4, 341.1, 346.8, 352.5, 358.2, 363.9, 369.6,\n",
       "       375.3, 381.1, 386.9, 392.7, 398.5, 404.3, 410.1, 415.9, 421.8,\n",
       "       427.7, 433.6, 439.6, 445.6, 451.6, 457.6, 463.6, 469.6, 475.7,\n",
       "       481.8, 487.9, 494. , 500.1, 506.2, 512.4, 518.6, 524.8, 531. ,\n",
       "       537.3, 543.6, 549.9, 556.2, 562.5, 568.8, 575.1, 581.4, 587.7,\n",
       "       594.1, 600.5, 606.9, 613.3, 619.7, 626.1, 632.5, 639. , 645.5,\n",
       "       652. , 658.5, 665. , 671.6, 678.2, 684.9, 691.6, 698.3, 705. ,\n",
       "       711.7, 718.4, 725.1, 731.8, 738.6, 745.4, 752.2, 759.1, 766. ,\n",
       "       772.9, 779.8, 786.8, 793.9, 801.1, 808.3, 815.5, 822.8, 830.2,\n",
       "       837.8, 845.5, 853.2, 860.9, 868.6, 876.5])"
      ]
     },
     "execution_count": 294,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.cumsum(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 295,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5.843333333333334"
      ]
     },
     "execution_count": 295,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " np.mean(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 296,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.8253012917851409"
      ]
     },
     "execution_count": 296,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.std(data)# 标准差"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 297,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.6811222222222223"
      ]
     },
     "execution_count": 297,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " np.var(data)# ⽅差"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 298,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4.3"
      ]
     },
     "execution_count": 298,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " np.min(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "254px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
